﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
// Developer:  Michael Antonio
using System;

namespace MichMan.Utilities.Requests
{

    /// <summary>
    /// Wraps a cache.
    /// </summary>
    public abstract class CacheWrapper : CacheBase, ICache
    {
        /// <summary>
        /// Called once to get the wrapped cache.
        /// </summary>
        /// <returns></returns>
        protected abstract ICache GetWrapped();

        private ICache _wrapped = null;
        
        protected ICache Wrapped
        {
            get { return _wrapped ?? (_wrapped = GetWrapped()); }
        }

        /// <summary>
        /// Gets the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public virtual object Get(string key)
        {
            return Wrapped == null ? null : Wrapped.Get(key);
        }

        /// <summary>
        /// Inserts the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="o">The object.</param>
        /// <param name="lifetime">The lifetime.</param>
        /// <returns></returns>
        public virtual bool Insert(string key, object o, TimeSpan? lifetime)
        {
            return Wrapped != null && Wrapped.Insert(key, o, lifetime);
        }

        /// <summary>
        /// Deletes the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        public virtual void Delete(string key)
        {
            if (Wrapped != null)
            {
                Wrapped.Delete(key);
            }
        }
        /// <summary>
        /// Removes everything from the cache.
        /// </summary>
        public virtual void Clear()
        {
            if (Wrapped != null)
            {
                Wrapped.Clear();
            }
        }

    }

}
